﻿namespace Softserve.Pottery.MainApplication
{

    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml;
    using Softserve.Pottery.Manufacturing;
    using Softserve.Pottery.Ordering.Domains;
    using Softserve.Pottery.Store;
    using Softserve.Pottery.Warehouse;



    public class ModelSerializator
    {
        /*const string orderPath = @"..\..\XML\Orders.xml";
        const string batchesPath = @"..\..\XML\Batches.xml";
        const string materialsPath = @"..\..\XML\Materials.xml";
        const string cardsPath = @"..\..\XML\PartnerCards.xml";
        const string productsPath = @"..\..\XML\Products.xml";
        const string usersPath = @"..\..\XML\Users.xml";
        const string warehousesPath = @"..\..\XML\Warehouses.xml";

        internal DomainsContainer DomainsData
        {
            get { return m_DomainsData; }
            set { m_DomainsData = value; }
        }

        public ModelSerializator()
        {
            Init();
        }

        private void Init()
        {
        //    DomainsData.Orders = new List<Order>()
        //    {
        //        new Order() {Id = 1, DateCreate = new DateTime(2012, 9, 10), DeliveryType = 1, Priority = 1, Status = 1, TotalSum = 1100, UserId = 1},
        //        new Order() {Id = 2, DateCreate = new DateTime(2012, 9, 11), DeliveryType = 2, Priority = 2, Status = 2, TotalSum = 990, UserId = 2},
        //        new Order() {Id = 3, DateCreate = new DateTime(2012, 9, 12), DeliveryType = 1, Priority = 2, Status = 1, TotalSum = 5000, UserId = 3},
        //        new Order() {Id = 4, DateCreate = new DateTime(2012, 9, 13), DeliveryType = 2, Priority = 3, Status = 2, TotalSum = 2300, UserId = 4},
        //        new Order() {Id = 5, DateCreate = new DateTime(2012, 9, 14), DeliveryType = 1, Priority = 1, Status = 1, TotalSum = 5700, UserId = 5}
        //    };

        //    DomainsData.Batches = new List<Batch>()
        //    {
        //        new Batch(){Id = 1, DateCreate = new DateTime(2012, 9, 12)},
        //        new Batch(){Id = 2, DateCreate = new DateTime(2012, 9, 13)},
        //        new Batch(){Id = 3, DateCreate = new DateTime(2012, 9, 14)},
        //        new Batch(){Id = 4, DateCreate = new DateTime(2012, 9, 15)},
        //        new Batch(){Id = 5, DateCreate = new DateTime(2012, 9, 16)}
        //    };

        //    DomainsData.Materials = new List<Material>()
        //    {
        //        new Material(){Id = 1, Name = "Red clay"},
        //        new Material(){Id = 2, Name = "White clay"},
        //        new Material(){Id = 3, Name = "Red dye"},
        //        new Material(){Id = 4, Name = "Green dye"},
        //        new Material(){Id = 5, Name = "Blue dye"}
        //    };

        //    DomainsData.PartnerCards = new List<PartnerCard>()
        //    {
        //        new PartnerCard(){Id = 1 , DateCreate = new DateTime(2012, 9, 10), DiscountPercent = 0.05, Number = "1234567890ABCDDEFG"},
        //        new PartnerCard(){Id = 1 , DateCreate = new DateTime(2012, 9, 11), DiscountPercent = 0.05, Number = "2234567890ABCDDEFG"},
        //        new PartnerCard(){Id = 1 , DateCreate = new DateTime(2012, 9, 12), DiscountPercent = 0.05, Number = "3234567890ABCDDEFG"},
        //        new PartnerCard(){Id = 1 , DateCreate = new DateTime(2012, 9, 13), DiscountPercent = 0.05, Number = "4234567890ABCDDEFG"},
        //        new PartnerCard(){Id = 1 , DateCreate = new DateTime(2012, 9, 14), DiscountPercent = 0.05, Number = "5234567890ABCDDEFG"}
        //    };

        //    DomainsData.Products = new List<Product>()
        //    {
        //        new Product(){CostPrice = 22, CustomImgPath = "", Description = "", Id = 1, ImagePath = "", IsActive = true, IsSimple = true, Marking = "Plate", Price = 35, Size = "size"},
        //        new Product(){CostPrice = 32, CustomImgPath = "", Description = "", Id = 2, ImagePath = "", IsActive = true, IsSimple = true, Marking = "Pot", Price = 45, Size = "size"},
        //        new Product(){CostPrice = 42, CustomImgPath = "", Description = "", Id = 3, ImagePath = "", IsActive = true, IsSimple = true, Marking = "Cup", Price = 55, Size = "size"},
        //        new Product(){CostPrice = 52, CustomImgPath = "", Description = "", Id = 4, ImagePath = "", IsActive = true, IsSimple = true, Marking = "Big plate", Price = 65, Size = "size"},
        //        new Product(){CostPrice = 62, CustomImgPath = "", Description = "", Id = 5, ImagePath = "", IsActive = true, IsSimple = true, Marking = "Big cup", Price = 75, Size = "size"}
        //    };

        //    DomainsData.Users = new List<User>()
        //    {
        //        new User(){CumulativeSum = 2500, DateCreate = new DateTime(2012, 9, 10), Id = 1, PartnerCardId = 1, Status = 1},
        //        new User(){CumulativeSum = 2600, DateCreate = new DateTime(2012, 9, 11), Id = 2, PartnerCardId = 2, Status = 1},
        //        new User(){CumulativeSum = 2700, DateCreate = new DateTime(2012, 9, 12), Id = 3, PartnerCardId = 3, Status = 1},
        //        new User(){CumulativeSum = 2800, DateCreate = new DateTime(2012, 9, 13), Id = 4, PartnerCardId = 4, Status = 1},
        //        new User(){CumulativeSum = 2900, DateCreate = new DateTime(2012, 9, 14), Id = 5, PartnerCardId = 5, Status = 1}
        //    };

        //    DomainsData.WarehouseList = new List<Storage>()
        //    {
        //        new Storage(){Id = 1, Address = "Wonderland, 1 Wonder str.", DateCreate = new DateTime(2012, 9, 9), Name = "W1", SquareTotal = 100500, Status = 1},
        //        new Storage(){Id = 1, Address = "Wonderland, 5 East str.", DateCreate = new DateTime(2012, 9, 9), Name = "W2", SquareTotal = 100600, Status = 1}
        //    };
        }

        public void Serialize()
        {
            SerializeOrder(DomainsData.Orders, orderPath);
            SerializeOrder(DomainsData.Batches, batchesPath);
            SerializeOrder(DomainsData.Materials, materialsPath);
            SerializeOrder(DomainsData.Products, productsPath);
            SerializeOrder(DomainsData.Users, usersPath);
            SerializeOrder(DomainsData.WarehouseList, warehousesPath);
        }

        public DomainsContainer Deserialize()
        {
            XmlReaderSettings readerSetting = new XmlReaderSettings() { };
            DomainsContainer dc = new DomainsContainer();

            DataContractSerializer ds = new DataContractSerializer(typeof(List<Order>));
            using (var r = XmlReader.Create(orderPath, readerSetting))
                dc.Orders = ds.ReadObject(r) as List<Order>;

            ds = new DataContractSerializer(typeof(List<Batch>));
            using (var r = XmlReader.Create(batchesPath, readerSetting))
                dc.Batches = ds.ReadObject(r) as List<Batch>;

            ds = new DataContractSerializer(typeof(List<Material>));
            using (var r = XmlReader.Create(materialsPath, readerSetting))
                dc.Materials = ds.ReadObject(r) as List<Material>;

            ds = new DataContractSerializer(typeof(List<Product>));
            using (var r = XmlReader.Create(productsPath, readerSetting))
                dc.Products = ds.ReadObject(r) as List<Product>;

            ds = new DataContractSerializer(typeof(List<User>));
            using (var r = XmlReader.Create(usersPath, readerSetting))
                dc.Users = ds.ReadObject(r) as List<User>;

            ds = new DataContractSerializer(typeof(List<Warehouse>));
            using (var r = XmlReader.Create(warehousesPath, readerSetting))
                dc.WarehouseList = ds.ReadObject(r) as List<Warehouse>;

            return dc;
        }

        private void SerializeOrder(object lst, string path)
        {
            DataContractSerializer sr = new DataContractSerializer(lst.GetType());
            XmlWriterSettings writerSettings = new XmlWriterSettings() { Indent = true };
            using (XmlWriter xwr = XmlWriter.Create(path, writerSettings))
            {
                sr.WriteObject(xwr, lst);
            }
        }

        #region Private members

        DomainsContainer m_DomainsData = new DomainsContainer();

        #endregion*/
    }
}
